#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
//初始化顺序表
void SeqListInit(SeqList* ps)
{
	assert(ps);
	ps->size = 0;
	ps->capacity = 0;
	ps->a= NULL;
}
//销毁顺序表
void SeqListDestory(SeqList* ps)
{
	assert(ps);
	if (ps->a)
	{
		free(ps->a);
		ps->a = NULL;
		ps->capacity = 0;
		ps->size = 0;
	}
}
//扩容操作
void SeqListCheckCapacity(SeqList* ps)
{
	assert(ps);
	if (ps->size == ps->capacity)
	{
		size_t new_capacity = ps->capacity == 0 ? 5 : ps->capacity * 2;
		SLDateType* tmp = (SLDateType*)realloc(ps->a, sizeof(SLDateType) * new_capacity);
		if (tmp == NULL)
		{
			perror("realloc:");
			exit(-1);
		}
		else
		{
			ps->a = tmp;
			ps->capacity = new_capacity;
		}
	}	
}
//打印顺序表
void SeqListPrint(SeqList* ps)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->a[i]);
	}
	printf("\n");
}
//尾插
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	//assert(ps);
	//SeqListCheckCapacity(ps);
	//ps->a[ps->size] = x;
	//ps->size++;
	SeqListInsert(ps, ps->size, x);

}
//头插
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	//assert(ps);
	//SeqListCheckCapacity(ps);
	//for (int i = ps->size; i > 0; i--)
	//{
	//	ps->a[i] = ps->a[i - 1];
	//}
	//ps->a[0] = x;
	//ps->size++;
	SeqListInsert(ps, 0, x);
}
//头删
void SeqListPopFront(SeqList* ps)
{
	//assert(ps);
	//for (int i = 1; i < ps->size; i++)
	//{
	//	ps->a[i - 1] = ps->a[i];
	//}
	//ps->size--;
	SeqListErase(ps, 0);

}
//尾删
void SeqListPopBack(SeqList* ps)
{
	//assert(ps);
	//ps->size--;
	SeqListErase(ps, ps->size-1);
}
//顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (x == ps->a[i])
		{
			return i;
		}
	}
	return -1;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
	assert(ps);
	SeqListCheckCapacity(ps);
	int tmp = ps->size;
	while (tmp != pos)
	{
		ps->a[tmp] = ps->a[tmp - 1];
		tmp--;
	}
	ps->a[tmp] = x;
	ps->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
	assert(ps);
	for (int i=pos+1; i < ps->size; i++)
	{
		ps->a[i-1] = ps->a[i];
	}
	ps->size--;
}
